﻿using System;

namespace wz.common
{
    public class UnixTimeStamp
    {
        /// <summary>
        /// 时间戳的.NET时间
        /// </summary>
        public DateTime Value { get; private set; }
        public string @String { get; private set; }

        /// <summary>
        /// 获取时间戳到当前时间的时间差
        /// Now-this
        /// </summary>
        public TimeSpan GetTimeDiff()
        {
            return DateTime.Now - Value;
        }

        public UnixTimeStamp(string timestamp)
        {
            @String = timestamp;
            string timeStamp = timestamp;
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime signTime = dtStart.Add(toNow);
            Value = signTime;
        }

        public static UnixTimeStamp Now
        {
            get
            {
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
                TimeSpan toNow = dtNow.Subtract(dtStart);
                string timeStamp = toNow.Ticks.ToString();
                timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
                return new UnixTimeStamp(timeStamp);
            }
        }

        public override string ToString()
        {
            return @String;
        }
    }
}